{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "-ETtu9CvVMDR"
   },
   "source": [
    "# Chapter 6 - Prompt Engineering\n",
    "通过提示工程提高生成输出的质量\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-10-12T08:48:54.707170Z",
     "iopub.status.busy": "2024-10-12T08:48:54.706943Z",
     "iopub.status.idle": "2024-10-12T08:48:54.711073Z",
     "shell.execute_reply": "2024-10-12T08:48:54.710296Z",
     "shell.execute_reply.started": "2024-10-12T08:48:54.707154Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "# %%capture\n",
    "# !pip install langchain>=0.1.17 openai>=1.13.3 langchain_openai>=0.1.6 transformers>=4.40.1 datasets>=2.18.0 accelerate>=0.27.2 sentence-transformers>=2.5.1 duckduckgo-search>=5.2.2\n",
    "# !CMAKE_ARGS=\"-DLLAMA_CUBLAS=on\" pip install llama-cpp-python"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-10-12T08:48:56.021509Z",
     "iopub.status.busy": "2024-10-12T08:48:56.021302Z",
     "iopub.status.idle": "2024-10-12T08:48:56.027716Z",
     "shell.execute_reply": "2024-10-12T08:48:56.027153Z",
     "shell.execute_reply.started": "2024-10-12T08:48:56.021493Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "import os\n",
    "os.environ[\"HF_HOME\"] = \"/openbayes/home/huggingface\"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-10-12T08:48:57.238262Z",
     "iopub.status.busy": "2024-10-12T08:48:57.238054Z",
     "iopub.status.idle": "2024-10-12T08:48:57.460047Z",
     "shell.execute_reply": "2024-10-12T08:48:57.459037Z",
     "shell.execute_reply.started": "2024-10-12T08:48:57.238246Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "/openbayes/home/huggingface\n"
     ]
    }
   ],
   "source": [
    "!echo $HF_HOME"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "KaB0B1LdMcPM"
   },
   "source": [
    "## 6.1 加载模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 269,
     "referenced_widgets": [
      "977271302f104ebdb11c0a2a8183cc93",
      "931c6ca8360a4e79924399f017f5522d",
      "2ff276f9bbab440bb036c1109c023582",
      "6d5243b8997d4f64af62900cb19489d0",
      "680d3647069b45c0b4353548ac52ef37",
      "0512bf845dda462395f58c06dfb0733d",
      "26985bad36884566a7bd4ba6f1037e80",
      "e174502c4dfe4740922264d67a9a74b5",
      "9da5552b33c1418d9e0190ec66f19c9d",
      "33acfbdb950545a6a67a9b39be02e30b",
      "c6d9399532c14ce58189b9d038e4cfa4"
     ]
    },
    "execution": {
     "iopub.execute_input": "2024-10-12T08:50:03.424415Z",
     "iopub.status.busy": "2024-10-12T08:50:03.424115Z",
     "iopub.status.idle": "2024-10-12T08:56:53.936611Z",
     "shell.execute_reply": "2024-10-12T08:56:53.936117Z",
     "shell.execute_reply.started": "2024-10-12T08:50:03.424398Z"
    },
    "executionInfo": {
     "elapsed": 58517,
     "status": "ok",
     "timestamp": 1715083822638,
     "user": {
      "displayName": "Maarten Grootendorst",
      "userId": "11015108362723620659"
     },
     "user_tz": -120
    },
    "id": "mNkbw28oMeAM",
    "outputId": "c0b97b59-180f-427f-8d49-398cb1b39c40",
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "81bef95ac2fc4d1b91b1ff1831666408",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "config.json:   0%|          | 0.00/663 [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "0b93fca4695e413dbe2d52a4fb75602e",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "model.safetensors.index.json:   0%|          | 0.00/27.8k [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "664410d881e44e0dbf6fedd1aaf36bc1",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Downloading shards:   0%|          | 0/4 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "36b93ef76303406d9297eb613305b7ef",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "model-00001-of-00004.safetensors:   0%|          | 0.00/3.95G [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "bb982cad28b445d89da3aaa7358b0bac",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "model-00002-of-00004.safetensors:   0%|          | 0.00/3.86G [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "136860e75b284fb383cb538f6ce6902d",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "model-00003-of-00004.safetensors:   0%|          | 0.00/3.86G [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "09540a76c2ef4a4c9f0a87d21b0b7f00",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "model-00004-of-00004.safetensors:   0%|          | 0.00/3.56G [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "f5f49807b4a94d0da6a6b084fb0426a3",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Loading checkpoint shards:   0%|          | 0/4 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "e474fd67063c4c9bb4eb5fcf4514baff",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "generation_config.json:   0%|          | 0.00/243 [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "688be54a8e5a41d39b739e4132313d88",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "tokenizer_config.json:   0%|          | 0.00/7.30k [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "f1e85f267ebe45c79075e3fbf854f414",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "vocab.json:   0%|          | 0.00/2.78M [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "8744bca0c49a40c1928cdc6ce7e10e1b",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "merges.txt:   0%|          | 0.00/1.67M [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "2aa69e21ceb742f5a3d938dd6a8f5518",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "tokenizer.json:   0%|          | 0.00/7.03M [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Special tokens have been added in the vocabulary, make sure the associated word embeddings are fine-tuned or trained.\n"
     ]
    }
   ],
   "source": [
    "import torch\n",
    "from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline\n",
    "\n",
    "\n",
    "model_name = \"Qwen/Qwen2.5-7B-Instruct\"\n",
    "# model_name = \"Qwen/Qwen2.5-0.5B-Instruct\"\n",
    "\n",
    "# Load model and tokenizer\n",
    "model = AutoModelForCausalLM.from_pretrained(\n",
    "    model_name, \n",
    "    device_map=\"cuda\",\n",
    "    torch_dtype=\"auto\",\n",
    "    trust_remote_code=True,\n",
    ")\n",
    "tokenizer = AutoTokenizer.from_pretrained(model_name)\n",
    "\n",
    "# Create a pipeline\n",
    "pipe = pipeline(\n",
    "    \"text-generation\",\n",
    "    model=model,\n",
    "    tokenizer=tokenizer,\n",
    "    return_full_text=False,\n",
    "    max_new_tokens=500,\n",
    "    do_sample=False,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "execution": {
     "iopub.execute_input": "2024-10-12T09:01:15.554888Z",
     "iopub.status.busy": "2024-10-12T09:01:15.554660Z",
     "iopub.status.idle": "2024-10-12T09:01:18.161979Z",
     "shell.execute_reply": "2024-10-12T09:01:18.161061Z",
     "shell.execute_reply.started": "2024-10-12T09:01:15.554870Z"
    },
    "executionInfo": {
     "elapsed": 3374,
     "status": "ok",
     "timestamp": 1715083826011,
     "user": {
      "displayName": "Maarten Grootendorst",
      "userId": "11015108362723620659"
     },
     "user_tz": -120
    },
    "id": "uZIMVL0Q3g8P",
    "outputId": "2df55f98-fcf7-448e-b4de-3ae1ed8c1a5f",
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "当然可以，这里有一个关于猫和狗的小笑话：\n",
      "\n",
      "有一天，猫和狗决定去参加一场才艺大赛。猫非常自信地展示了自己的抓老鼠技能，而狗则兴奋地表演了接飞盘。\n",
      "\n",
      "最后，评委宣布了结果：猫获得了第二名，而狗获得了第一名。\n",
      "\n",
      "猫疑惑地问狗：“为什么我拿了第二名？”\n",
      "\n",
      "狗得意地说：“因为我是唯一一个没有把飞盘还给主人的。”\n",
      "\n",
      "这个笑话虽然简单，但通过对比猫和狗的特点，展现了一种轻松幽默的氛围。希望你喜欢！\n"
     ]
    }
   ],
   "source": [
    "#  简要的 prompt\n",
    "messages = [\n",
    "    {\"role\": \"user\", \"content\": \"讲一个猫和狗有关的笑话.\"}\n",
    "]\n",
    "\n",
    "# 生成结果\n",
    "output = pipe(messages)\n",
    "print(output[0][\"generated_text\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "execution": {
     "iopub.execute_input": "2024-10-12T08:59:12.172191Z",
     "iopub.status.busy": "2024-10-12T08:59:12.171972Z",
     "iopub.status.idle": "2024-10-12T08:59:12.176618Z",
     "shell.execute_reply": "2024-10-12T08:59:12.175797Z",
     "shell.execute_reply.started": "2024-10-12T08:59:12.172174Z"
    },
    "executionInfo": {
     "elapsed": 8,
     "status": "ok",
     "timestamp": 1715083826011,
     "user": {
      "displayName": "Maarten Grootendorst",
      "userId": "11015108362723620659"
     },
     "user_tz": -120
    },
    "id": "YOGpAY8w4odt",
    "outputId": "2cc5de39-2a60-4ec4-9894-72b7b95a2fbb",
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<|im_start|>system\n",
      "You are Qwen, created by Alibaba Cloud. You are a helpful assistant.<|im_end|>\n",
      "<|im_start|>user\n",
      "讲一个猫和狗有关的笑话.<|im_end|>\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# Apply prompt template\n",
    "prompt = pipe.tokenizer.apply_chat_template(messages, tokenize=False)\n",
    "print(prompt)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "execution": {
     "iopub.execute_input": "2024-10-12T08:59:23.131326Z",
     "iopub.status.busy": "2024-10-12T08:59:23.130888Z",
     "iopub.status.idle": "2024-10-12T08:59:24.161718Z",
     "shell.execute_reply": "2024-10-12T08:59:24.160848Z",
     "shell.execute_reply.started": "2024-10-12T08:59:23.131289Z"
    },
    "executionInfo": {
     "elapsed": 3497,
     "status": "ok",
     "timestamp": 1715083829501,
     "user": {
      "displayName": "Maarten Grootendorst",
      "userId": "11015108362723620659"
     },
     "user_tz": -120
    },
    "id": "eLd_XXaR3i04",
    "outputId": "fa46605a-6e0f-4647-d725-b2ee5f2528b7",
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "当然可以，这里有一个关于猫和狗的小笑话：\n",
      "\n",
      "为什么猫不喜欢上网？\n",
      "\n",
      "因为它们总是被“狗狗视频”占用时间！\n"
     ]
    }
   ],
   "source": [
    "# Using a high temperature\n",
    "output = pipe(messages, do_sample=True, temperature=1)\n",
    "print(output[0][\"generated_text\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "execution": {
     "iopub.execute_input": "2024-10-12T08:59:28.891959Z",
     "iopub.status.busy": "2024-10-12T08:59:28.891249Z",
     "iopub.status.idle": "2024-10-12T08:59:30.064424Z",
     "shell.execute_reply": "2024-10-12T08:59:30.063790Z",
     "shell.execute_reply.started": "2024-10-12T08:59:28.891937Z"
    },
    "executionInfo": {
     "elapsed": 1813,
     "status": "ok",
     "timestamp": 1715083831313,
     "user": {
      "displayName": "Maarten Grootendorst",
      "userId": "11015108362723620659"
     },
     "user_tz": -120
    },
    "id": "sz61Fvtk580U",
    "outputId": "38839183-60f7-49cc-f695-11841d116a7e",
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "当然可以！这里有一个关于猫和狗的笑话：\n",
      "\n",
      "为什么猫会嫉妒狗狗收到的生日卡上写着“最好的朋友”？\n",
      "\n",
      "因为猫想，如果它也是“最好的朋友”，那就没人再把它当成“沙发的一部分”了！\n"
     ]
    }
   ],
   "source": [
    "# Using a high top_p\n",
    "output = pipe(messages, do_sample=True, top_p=1)\n",
    "print(output[0][\"generated_text\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "K7oqc_Qj2008"
   },
   "source": [
    "## 6.2 提示词工程"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-10-12T09:04:23.124733Z",
     "iopub.status.busy": "2024-10-12T09:04:23.124505Z",
     "iopub.status.idle": "2024-10-12T09:04:23.128588Z",
     "shell.execute_reply": "2024-10-12T09:04:23.127879Z",
     "shell.execute_reply.started": "2024-10-12T09:04:23.124716Z"
    },
    "tags": []
   },
   "source": [
    "Prompt Engineering 在网络上的资源非常多，个人觉得看吴恩达的《https://github.com/Kevin-free/chatgpt-prompt-engineering-for-developers》基本就够了"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "lnojy54u2uQa"
   },
   "source": [
    "## 6.3 高级提示词工程\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "NMF9p8qK58Ou"
   },
   "source": [
    "### 6.3.1 复杂详细的 prompt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-10-12T09:07:16.423308Z",
     "iopub.status.busy": "2024-10-12T09:07:16.422918Z",
     "iopub.status.idle": "2024-10-12T09:07:16.431803Z",
     "shell.execute_reply": "2024-10-12T09:07:16.431027Z",
     "shell.execute_reply.started": "2024-10-12T09:07:16.423272Z"
    },
    "id": "uEQ9ZnSH73yP",
    "tags": []
   },
   "outputs": [],
   "source": [
    "# Text to summarize which we stole from https://jalammar.github.io/illustrated-transformer/ ;)\n",
    "text = \"\"\"In the previous post, we looked at Attention – a ubiquitous method in modern deep learning models. Attention is a concept that helped improve the performance of neural machine translation applications. In this post, we will look at The Transformer – a model that uses attention to boost the speed with which these models can be trained. The Transformer outperforms the Google Neural Machine Translation model in specific tasks. The biggest benefit, however, comes from how The Transformer lends itself to parallelization. It is in fact Google Cloud’s recommendation to use The Transformer as a reference model to use their Cloud TPU offering. So let’s try to break the model apart and look at how it functions.\n",
    "The Transformer was proposed in the paper Attention is All You Need. A TensorFlow implementation of it is available as a part of the Tensor2Tensor package. Harvard’s NLP group created a guide annotating the paper with PyTorch implementation. In this post, we will attempt to oversimplify things a bit and introduce the concepts one by one to hopefully make it easier to understand to people without in-depth knowledge of the subject matter.\n",
    "Let’s begin by looking at the model as a single black box. In a machine translation application, it would take a sentence in one language, and output its translation in another.\n",
    "Popping open that Optimus Prime goodness, we see an encoding component, a decoding component, and connections between them.\n",
    "The encoding component is a stack of encoders (the paper stacks six of them on top of each other – there’s nothing magical about the number six, one can definitely experiment with other arrangements). The decoding component is a stack of decoders of the same number.\n",
    "The encoders are all identical in structure (yet they do not share weights). Each one is broken down into two sub-layers:\n",
    "The encoder’s inputs first flow through a self-attention layer – a layer that helps the encoder look at other words in the input sentence as it encodes a specific word. We’ll look closer at self-attention later in the post.\n",
    "The outputs of the self-attention layer are fed to a feed-forward neural network. The exact same feed-forward network is independently applied to each position.\n",
    "The decoder has both those layers, but between them is an attention layer that helps the decoder focus on relevant parts of the input sentence (similar what attention does in seq2seq models).\n",
    "Now that we’ve seen the major components of the model, let’s start to look at the various vectors/tensors and how they flow between these components to turn the input of a trained model into an output.\n",
    "As is the case in NLP applications in general, we begin by turning each input word into a vector using an embedding algorithm.\n",
    "Each word is embedded into a vector of size 512. We'll represent those vectors with these simple boxes.\n",
    "The embedding only happens in the bottom-most encoder. The abstraction that is common to all the encoders is that they receive a list of vectors each of the size 512 – In the bottom encoder that would be the word embeddings, but in other encoders, it would be the output of the encoder that’s directly below. The size of this list is hyperparameter we can set – basically it would be the length of the longest sentence in our training dataset.\n",
    "After embedding the words in our input sequence, each of them flows through each of the two layers of the encoder.\n",
    "Here we begin to see one key property of the Transformer, which is that the word in each position flows through its own path in the encoder. There are dependencies between these paths in the self-attention layer. The feed-forward layer does not have those dependencies, however, and thus the various paths can be executed in parallel while flowing through the feed-forward layer.\n",
    "Next, we’ll switch up the example to a shorter sentence and we’ll look at what happens in each sub-layer of the encoder.\n",
    "Now We’re Encoding!\n",
    "As we’ve mentioned already, an encoder receives a list of vectors as input. It processes this list by passing these vectors into a ‘self-attention’ layer, then into a feed-forward neural network, then sends out the output upwards to the next encoder.\n",
    "\"\"\"\n",
    "\n",
    "# Prompt components\n",
    "persona = \"You are an expert in Large Language models. You excel at breaking down complex papers into digestible summaries.\\n\"\n",
    "instruction = \"Summarize the key findings of the paper provided.\\n\"\n",
    "context = \"Your summary should extract the most crucial points that can help researchers quickly understand the most vital information of the paper.\\n\"\n",
    "data_format = \"Create a bullet-point summary that outlines the method. Follow this up with a concise paragraph that encapsulates the main results.\\n\"\n",
    "audience = \"The summary is designed for busy researchers that quickly need to grasp the newest trends in Large Language Models.\\n\"\n",
    "tone = \"The tone should be professional and clear.\\n\"\n",
    "# text = \"MY TEXT TO SUMMARIZE\"  # Replace with your own text to summarize\n",
    "data = f\"Text to summarize: {text}\"\n",
    "\n",
    "# The full prompt - remove and add pieces to view its impact on the generated output\n",
    "query = persona + instruction + context + data_format + audience + tone + data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "execution": {
     "iopub.execute_input": "2024-10-12T09:07:32.147967Z",
     "iopub.status.busy": "2024-10-12T09:07:32.147537Z",
     "iopub.status.idle": "2024-10-12T09:07:32.153484Z",
     "shell.execute_reply": "2024-10-12T09:07:32.152913Z",
     "shell.execute_reply.started": "2024-10-12T09:07:32.147924Z"
    },
    "executionInfo": {
     "elapsed": 2,
     "status": "ok",
     "timestamp": 1715083839583,
     "user": {
      "displayName": "Maarten Grootendorst",
      "userId": "11015108362723620659"
     },
     "user_tz": -120
    },
    "id": "Y8hbXm9R-sr0",
    "outputId": "a4b921a4-909c-4ad8-c3c7-eaf01c9652ef",
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<|im_start|>system\n",
      "You are Qwen, created by Alibaba Cloud. You are a helpful assistant.<|im_end|>\n",
      "<|im_start|>user\n",
      "You are an expert in Large Language models. You excel at breaking down complex papers into digestible summaries.\n",
      "Summarize the key findings of the paper provided.\n",
      "Your summary should extract the most crucial points that can help researchers quickly understand the most vital information of the paper.\n",
      "Create a bullet-point summary that outlines the method. Follow this up with a concise paragraph that encapsulates the main results.\n",
      "The summary is designed for busy researchers that quickly need to grasp the newest trends in Large Language Models.\n",
      "The tone should be professional and clear.\n",
      "Text to summarize: In the previous post, we looked at Attention – a ubiquitous method in modern deep learning models. Attention is a concept that helped improve the performance of neural machine translation applications. In this post, we will look at The Transformer – a model that uses attention to boost the speed with which these models can be trained. The Transformer outperforms the Google Neural Machine Translation model in specific tasks. The biggest benefit, however, comes from how The Transformer lends itself to parallelization. It is in fact Google Cloud’s recommendation to use The Transformer as a reference model to use their Cloud TPU offering. So let’s try to break the model apart and look at how it functions.\n",
      "The Transformer was proposed in the paper Attention is All You Need. A TensorFlow implementation of it is available as a part of the Tensor2Tensor package. Harvard’s NLP group created a guide annotating the paper with PyTorch implementation. In this post, we will attempt to oversimplify things a bit and introduce the concepts one by one to hopefully make it easier to understand to people without in-depth knowledge of the subject matter.\n",
      "Let’s begin by looking at the model as a single black box. In a machine translation application, it would take a sentence in one language, and output its translation in another.\n",
      "Popping open that Optimus Prime goodness, we see an encoding component, a decoding component, and connections between them.\n",
      "The encoding component is a stack of encoders (the paper stacks six of them on top of each other – there’s nothing magical about the number six, one can definitely experiment with other arrangements). The decoding component is a stack of decoders of the same number.\n",
      "The encoders are all identical in structure (yet they do not share weights). Each one is broken down into two sub-layers:\n",
      "The encoder’s inputs first flow through a self-attention layer – a layer that helps the encoder look at other words in the input sentence as it encodes a specific word. We’ll look closer at self-attention later in the post.\n",
      "The outputs of the self-attention layer are fed to a feed-forward neural network. The exact same feed-forward network is independently applied to each position.\n",
      "The decoder has both those layers, but between them is an attention layer that helps the decoder focus on relevant parts of the input sentence (similar what attention does in seq2seq models).\n",
      "Now that we’ve seen the major components of the model, let’s start to look at the various vectors/tensors and how they flow between these components to turn the input of a trained model into an output.\n",
      "As is the case in NLP applications in general, we begin by turning each input word into a vector using an embedding algorithm.\n",
      "Each word is embedded into a vector of size 512. We'll represent those vectors with these simple boxes.\n",
      "The embedding only happens in the bottom-most encoder. The abstraction that is common to all the encoders is that they receive a list of vectors each of the size 512 – In the bottom encoder that would be the word embeddings, but in other encoders, it would be the output of the encoder that’s directly below. The size of this list is hyperparameter we can set – basically it would be the length of the longest sentence in our training dataset.\n",
      "After embedding the words in our input sequence, each of them flows through each of the two layers of the encoder.\n",
      "Here we begin to see one key property of the Transformer, which is that the word in each position flows through its own path in the encoder. There are dependencies between these paths in the self-attention layer. The feed-forward layer does not have those dependencies, however, and thus the various paths can be executed in parallel while flowing through the feed-forward layer.\n",
      "Next, we’ll switch up the example to a shorter sentence and we’ll look at what happens in each sub-layer of the encoder.\n",
      "Now We’re Encoding!\n",
      "As we’ve mentioned already, an encoder receives a list of vectors as input. It processes this list by passing these vectors into a ‘self-attention’ layer, then into a feed-forward neural network, then sends out the output upwards to the next encoder.\n",
      "<|im_end|>\n",
      "\n"
     ]
    }
   ],
   "source": [
    "messages = [\n",
    "    {\"role\": \"user\", \"content\": query}\n",
    "]\n",
    "print(tokenizer.apply_chat_template(messages, tokenize=False))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "execution": {
     "iopub.execute_input": "2024-10-12T09:07:35.929370Z",
     "iopub.status.busy": "2024-10-12T09:07:35.928944Z",
     "iopub.status.idle": "2024-10-12T09:07:43.226087Z",
     "shell.execute_reply": "2024-10-12T09:07:43.225142Z",
     "shell.execute_reply.started": "2024-10-12T09:07:35.929335Z"
    },
    "executionInfo": {
     "elapsed": 31865,
     "status": "ok",
     "timestamp": 1715073730373,
     "user": {
      "displayName": "Maarten Grootendorst",
      "userId": "11015108362723620659"
     },
     "user_tz": -120
    },
    "id": "-jO_uqNMTiWv",
    "outputId": "243160b8-c29a-4858-d72e-ac076b5cdc46",
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "### Summary of Key Method\n",
      "\n",
      "- **Model Overview**: The Transformer is a model that uses attention mechanisms to enhance the speed of training compared to traditional neural machine translation models.\n",
      "- **Architecture**:\n",
      "  - **Encoding Component**: Consists of a stack of identical encoders, each with two sub-layers: a self-attention layer and a feed-forward neural network.\n",
      "  - **Decoding Component**: Consists of a stack of decoders, each also with two sub-layers: a self-attention layer and a feed-forward neural network.\n",
      "  - **Connections**: Encoders and decoders are connected, allowing the decoder to attend to relevant parts of the encoded input.\n",
      "\n",
      "- **Embedding**: Each input word is converted into a vector of size 512 using an embedding algorithm.\n",
      "- **Parallelization**: The model's architecture allows for parallel processing, making it highly efficient for training and inference.\n",
      "\n",
      "### Main Results\n",
      "\n",
      "The Transformer model significantly improves upon existing neural machine translation systems by leveraging attention mechanisms to enhance both performance and training efficiency. Its unique architecture, characterized by a stack of identical encoders and decoders, enables parallel processing, which is particularly advantageous for large-scale training tasks. This design not only accelerates training but also makes the model more scalable and adaptable to different tasks. Additionally, the model's ability to handle long sequences efficiently, combined with its parallelizable nature, positions it as a strong candidate for various natural language processing applications. Notably, Google Cloud recommends using the Transformer as a reference model for their Cloud TPU offerings, highlighting its importance in cloud-based deep learning deployments.\n"
     ]
    }
   ],
   "source": [
    "# Generate the output\n",
    "outputs = pipe(messages)\n",
    "print(outputs[0][\"generated_text\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "Ol5aCUfM9bjM"
   },
   "source": [
    "### 6.3.2 In-Context Learning: 提供样例"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "execution": {
     "iopub.execute_input": "2024-10-12T09:08:31.652381Z",
     "iopub.status.busy": "2024-10-12T09:08:31.652128Z",
     "iopub.status.idle": "2024-10-12T09:08:31.657582Z",
     "shell.execute_reply": "2024-10-12T09:08:31.656865Z",
     "shell.execute_reply.started": "2024-10-12T09:08:31.652362Z"
    },
    "executionInfo": {
     "elapsed": 349,
     "status": "ok",
     "timestamp": 1715085868022,
     "user": {
      "displayName": "Maarten Grootendorst",
      "userId": "11015108362723620659"
     },
     "user_tz": -120
    },
    "id": "Ne92BtrXU8k8",
    "outputId": "ce08f1ae-8fb4-4c39-cb18-8d975f5ced31",
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<|im_start|>system\n",
      "You are Qwen, created by Alibaba Cloud. You are a helpful assistant.<|im_end|>\n",
      "<|im_start|>user\n",
      "A 'Gigamuru' is a type of Japanese musical instrument. An example of a sentence that uses the word Gigamuru is:<|im_end|>\n",
      "<|im_start|>assistant\n",
      "I have a Gigamuru that my uncle gave me as a gift. I love to play it at home.<|im_end|>\n",
      "<|im_start|>user\n",
      "To 'screeg' something is to swing a sword at it. An example of a sentence that uses the word screeg is:<|im_end|>\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# Use a single example of using the made-up word in a sentence\n",
    "one_shot_prompt = [\n",
    "    {\n",
    "        \"role\": \"user\",\n",
    "        \"content\": \"A 'Gigamuru' is a type of Japanese musical instrument. An example of a sentence that uses the word Gigamuru is:\"\n",
    "    },\n",
    "    {\n",
    "        \"role\": \"assistant\",\n",
    "        \"content\": \"I have a Gigamuru that my uncle gave me as a gift. I love to play it at home.\"\n",
    "    },\n",
    "    {\n",
    "        \"role\": \"user\",\n",
    "        \"content\": \"To 'screeg' something is to swing a sword at it. An example of a sentence that uses the word screeg is:\"\n",
    "    }\n",
    "]\n",
    "print(tokenizer.apply_chat_template(one_shot_prompt, tokenize=False))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "execution": {
     "iopub.execute_input": "2024-10-12T09:08:35.084381Z",
     "iopub.status.busy": "2024-10-12T09:08:35.083977Z",
     "iopub.status.idle": "2024-10-12T09:08:35.634997Z",
     "shell.execute_reply": "2024-10-12T09:08:35.634281Z",
     "shell.execute_reply.started": "2024-10-12T09:08:35.084346Z"
    },
    "executionInfo": {
     "elapsed": 3555,
     "status": "ok",
     "timestamp": 1715085873636,
     "user": {
      "displayName": "Maarten Grootendorst",
      "userId": "11015108362723620659"
     },
     "user_tz": -120
    },
    "id": "Ox3LeHW5JfaD",
    "outputId": "e803e67e-b0c2-4823-d1c9-d1f9bba1642b",
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The warrior screegged the air with his sword, testing its balance and weight before the battle began.\n"
     ]
    }
   ],
   "source": [
    "# Generate the output\n",
    "outputs = pipe(one_shot_prompt)\n",
    "print(outputs[0][\"generated_text\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "cugB9temhHJ0"
   },
   "source": [
    "### 6.3.3 Chain Prompting: 拆解问题\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "execution": {
     "iopub.execute_input": "2024-10-12T09:09:29.877181Z",
     "iopub.status.busy": "2024-10-12T09:09:29.876817Z",
     "iopub.status.idle": "2024-10-12T09:09:31.459698Z",
     "shell.execute_reply": "2024-10-12T09:09:31.458684Z",
     "shell.execute_reply.started": "2024-10-12T09:09:29.877154Z"
    },
    "executionInfo": {
     "elapsed": 3336,
     "status": "ok",
     "timestamp": 1715086284771,
     "user": {
      "displayName": "Maarten Grootendorst",
      "userId": "11015108362723620659"
     },
     "user_tz": -120
    },
    "id": "VXYuFn9eG2t4",
    "outputId": "3f96c196-8d37-44ca-fad5-94ddcbe8fc57",
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Certainly! Here's a name and slogan for a chatbot that leverages Large Language Models (LLMs):\n",
      "\n",
      "**Name:** LinguaBot\n",
      "\n",
      "**Slogan:** \"Unleash the Power of Language with LinguaBot\"\n",
      "\n",
      "This name and slogan aim to highlight the chatbot's ability to handle and generate human-like language through advanced LLM technology.\n"
     ]
    }
   ],
   "source": [
    "# Create name and slogan for a product\n",
    "product_prompt = [\n",
    "    {\"role\": \"user\", \"content\": \"Create a name and slogan for a chatbot that leverages LLMs.\"}\n",
    "]\n",
    "outputs = pipe(product_prompt)\n",
    "product_description = outputs[0][\"generated_text\"]\n",
    "print(product_description)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "execution": {
     "iopub.execute_input": "2024-10-12T09:09:38.847660Z",
     "iopub.status.busy": "2024-10-12T09:09:38.847427Z",
     "iopub.status.idle": "2024-10-12T09:09:39.581681Z",
     "shell.execute_reply": "2024-10-12T09:09:39.580864Z",
     "shell.execute_reply.started": "2024-10-12T09:09:38.847643Z"
    },
    "executionInfo": {
     "elapsed": 8805,
     "status": "ok",
     "timestamp": 1715086322608,
     "user": {
      "displayName": "Maarten Grootendorst",
      "userId": "11015108362723620659"
     },
     "user_tz": -120
    },
    "id": "fNYi3eDRG9Sk",
    "outputId": "0e1f5a80-27a4-42ef-b514-36e54dea708c",
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Introducing LinguaBot: Unleash the Power of Language with LinguaBot! Experience unparalleled conversation and language generation with our cutting-edge LLM technology.\n"
     ]
    }
   ],
   "source": [
    "# Based on a name and slogan for a product, generate a sales pitch\n",
    "sales_prompt = [\n",
    "    {\"role\": \"user\", \"content\": f\"Generate a very short sales pitch for the following product: '{product_description}'\"}\n",
    "]\n",
    "outputs = pipe(sales_prompt)\n",
    "sales_pitch = outputs[0][\"generated_text\"]\n",
    "print(sales_pitch)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "IL1UpiV02V5O"
   },
   "source": [
    "## 6.4 Reasoning with Generative Models\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "6lQJhAnY2W3O"
   },
   "source": [
    "### 6.4.1 Chain-of-Thought（CoT）: Think Before Answering \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "execution": {
     "iopub.execute_input": "2024-10-12T09:10:28.715006Z",
     "iopub.status.busy": "2024-10-12T09:10:28.714776Z",
     "iopub.status.idle": "2024-10-12T09:10:31.491238Z",
     "shell.execute_reply": "2024-10-12T09:10:31.490151Z",
     "shell.execute_reply.started": "2024-10-12T09:10:28.714990Z"
    },
    "executionInfo": {
     "elapsed": 6695,
     "status": "ok",
     "timestamp": 1715074858205,
     "user": {
      "displayName": "Maarten Grootendorst",
      "userId": "11015108362723620659"
     },
     "user_tz": -120
    },
    "id": "TnkxKmLyHGuR",
    "outputId": "3839ac23-9860-49b3-f769-20610786590a",
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Let's break it down step by step:\n",
      "\n",
      "1. The cafeteria started with 25 apples.\n",
      "2. They used 20 apples to make lunch. So, we subtract 20 from 25:\n",
      "   \\[\n",
      "   25 - 20 = 5\n",
      "   \\]\n",
      "   Now, they have 5 apples left.\n",
      "3. They then bought 6 more apples. So, we add 6 to the 5 apples they have left:\n",
      "   \\[\n",
      "   5 + 6 = 11\n",
      "   \\]\n",
      "\n",
      "Therefore, the cafeteria now has 11 apples.\n"
     ]
    }
   ],
   "source": [
    "# Answering without explicit reasoning\n",
    "standard_prompt = [\n",
    "    {\"role\": \"user\", \"content\": \"Roger has 5 tennis balls. He buys 2 more cans of tennis balls. Each can has 3 tennis balls. How many tennis balls does he have now?\"},\n",
    "    {\"role\": \"assistant\", \"content\": \"11\"},\n",
    "    {\"role\": \"user\", \"content\": \"The cafeteria had 25 apples. If they used 20 to make lunch and bought 6 more, how many apples do they have?\"}\n",
    "]\n",
    "\n",
    "# Run generative model\n",
    "outputs = pipe(standard_prompt)\n",
    "print(outputs[0][\"generated_text\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "execution": {
     "iopub.execute_input": "2024-10-12T09:10:35.239179Z",
     "iopub.status.busy": "2024-10-12T09:10:35.238946Z",
     "iopub.status.idle": "2024-10-12T09:10:38.037637Z",
     "shell.execute_reply": "2024-10-12T09:10:38.036773Z",
     "shell.execute_reply.started": "2024-10-12T09:10:35.239162Z"
    },
    "executionInfo": {
     "elapsed": 5409,
     "status": "ok",
     "timestamp": 1715086607857,
     "user": {
      "displayName": "Maarten Grootendorst",
      "userId": "11015108362723620659"
     },
     "user_tz": -120
    },
    "id": "X9jD3zPPEz_X",
    "outputId": "1b5a3d0f-1812-4299-eb95-11f9df48f7e7",
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Let's break it down step by step:\n",
      "\n",
      "1. The cafeteria started with 23 apples.\n",
      "2. They used 20 apples to make lunch. So, we subtract 20 from 23:\n",
      "   \\[\n",
      "   23 - 20 = 3\n",
      "   \\]\n",
      "   After making lunch, they have 3 apples left.\n",
      "3. They then bought 6 more apples. So, we add 6 to the 3 apples they have left:\n",
      "   \\[\n",
      "   3 + 6 = 9\n",
      "   \\]\n",
      "\n",
      "Therefore, the cafeteria now has 9 apples.\n"
     ]
    }
   ],
   "source": [
    "# Answering with chain-of-thought\n",
    "cot_prompt = [\n",
    "    {\"role\": \"user\", \"content\": \"Roger has 5 tennis balls. He buys 2 more cans of tennis balls. Each can has 3 tennis balls. How many tennis balls does he have now?\"},\n",
    "    {\"role\": \"assistant\", \"content\": \"Roger started with 5 balls. 2 cans of 3 tennis balls each is 6 tennis balls. 5 + 6 = 11. The answer is 11.\"},\n",
    "    {\"role\": \"user\", \"content\": \"The cafeteria had 23 apples. If they used 20 to make lunch and bought 6 more, how many apples do they have?\"}\n",
    "]\n",
    "\n",
    "# Generate the output\n",
    "outputs = pipe(cot_prompt)\n",
    "print(outputs[0][\"generated_text\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "4jVOOib72Z0P"
   },
   "source": [
    "### 6.4.2 Zero-shot Chain-of-Thought\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "execution": {
     "iopub.execute_input": "2024-10-12T09:10:50.523968Z",
     "iopub.status.busy": "2024-10-12T09:10:50.523745Z",
     "iopub.status.idle": "2024-10-12T09:10:54.027233Z",
     "shell.execute_reply": "2024-10-12T09:10:54.026348Z",
     "shell.execute_reply.started": "2024-10-12T09:10:50.523952Z"
    },
    "executionInfo": {
     "elapsed": 5802,
     "status": "ok",
     "timestamp": 1715086681529,
     "user": {
      "displayName": "Maarten Grootendorst",
      "userId": "11015108362723620659"
     },
     "user_tz": -120
    },
    "id": "iyHWAk2XKKGt",
    "outputId": "c6baf07b-5b72-4997-8499-aa8015303abd",
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Sure, let's break this down step-by-step:\n",
      "\n",
      "1. **Initial number of apples**: The cafeteria started with 23 apples.\n",
      "2. **Apples used for lunch**: They used 20 apples to make lunch. So, we subtract 20 from the initial 23 apples:\n",
      "   \\[\n",
      "   23 - 20 = 3\n",
      "   \\]\n",
      "   After making lunch, they have 3 apples left.\n",
      "3. **Apples bought**: They then bought 6 more apples. We add these 6 apples to the 3 apples they had left:\n",
      "   \\[\n",
      "   3 + 6 = 9\n",
      "   \\]\n",
      "\n",
      "So, after using some apples for lunch and buying more, the cafeteria now has 9 apples.\n"
     ]
    }
   ],
   "source": [
    "# Zero-shot Chain-of-Thought\n",
    "zeroshot_cot_prompt = [\n",
    "    {\"role\": \"user\", \"content\": \"The cafeteria had 23 apples. If they used 20 to make lunch and bought 6 more, how many apples do they have? Let's think step-by-step.\"}\n",
    "]\n",
    "\n",
    "# Generate the output\n",
    "outputs = pipe(zeroshot_cot_prompt)\n",
    "print(outputs[0][\"generated_text\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "-3-bMVLT2ehH"
   },
   "source": [
    "### 6.4.3 Tree-of-Thought: Exploring Intermediate Steps\n",
    "> 大家推测 openai-o1 就用了一个梳妆的思维链过程\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-10-12T09:11:50.935957Z",
     "iopub.status.busy": "2024-10-12T09:11:50.935726Z",
     "iopub.status.idle": "2024-10-12T09:11:50.940512Z",
     "shell.execute_reply": "2024-10-12T09:11:50.939662Z",
     "shell.execute_reply.started": "2024-10-12T09:11:50.935940Z"
    },
    "id": "3ZJo-4NBI1V-",
    "tags": []
   },
   "outputs": [],
   "source": [
    "# Zero-shot Chain-of-Thought\n",
    "zeroshot_tot_prompt = [\n",
    "    {\"role\": \"user\", \"content\": \"Imagine three different experts are answering this question. All experts will write down 1 step of their thinking, then share it with the group. Then all experts will go on to the next step, etc. If any expert realises they're wrong at any point then they leave. The question is 'The cafeteria had 23 apples. If they used 20 to make lunch and bought 6 more, how many apples do they have?' Make sure to discuss the results.\"}\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "execution": {
     "iopub.execute_input": "2024-10-12T09:11:55.965802Z",
     "iopub.status.busy": "2024-10-12T09:11:55.965502Z",
     "iopub.status.idle": "2024-10-12T09:12:04.898757Z",
     "shell.execute_reply": "2024-10-12T09:12:04.897668Z",
     "shell.execute_reply.started": "2024-10-12T09:11:55.965778Z"
    },
    "executionInfo": {
     "elapsed": 17866,
     "status": "ok",
     "timestamp": 1715086782469,
     "user": {
      "displayName": "Maarten Grootendorst",
      "userId": "11015108362723620659"
     },
     "user_tz": -120
    },
    "id": "qieQ9waXLbZO",
    "outputId": "6cfb9a7f-7b8f-4b0b-ac78-5aa4206175b8",
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Certainly! Let's break down the problem step-by-step with three experts, each sharing one step of their thinking before moving on to the next.\n",
      "\n",
      "### Expert 1: Initial Count and Usage\n",
      "\n",
      "**Step 1:** The cafeteria started with 23 apples.\n",
      "- **Expert 1's Thought:** \"We need to account for the apples that were used first.\"\n",
      "\n",
      "### Expert 2: Apples Used\n",
      "\n",
      "**Step 2:** They used 20 apples to make lunch.\n",
      "- **Expert 2's Thought:** \"After using 20 apples, we need to subtract this from the initial count.\"\n",
      "\n",
      "### Expert 3: Remaining Apples After Usage\n",
      "\n",
      "**Step 3:** Calculate the remaining apples after usage.\n",
      "- **Expert 3's Thought:** \"23 - 20 = 3 apples left.\"\n",
      "\n",
      "### Expert 1: New Purchase\n",
      "\n",
      "**Step 4:** They bought 6 more apples.\n",
      "- **Expert 1's Thought:** \"Now we need to add these new apples to the remaining ones.\"\n",
      "\n",
      "### Expert 2: Total Apples After Purchase\n",
      "\n",
      "**Step 5:** Calculate the total number of apples after the purchase.\n",
      "- **Expert 2's Thought:** \"3 + 6 = 9 apples in total.\"\n",
      "\n",
      "### Expert 3: Final Count\n",
      "\n",
      "**Step 6:** Verify the final count.\n",
      "- **Expert 3's Thought:** \"The final count should be 9 apples.\"\n",
      "\n",
      "### Discussion of Results\n",
      "\n",
      "All three experts have arrived at the same conclusion through their individual steps:\n",
      "\n",
      "- **Initial Count:** 23 apples\n",
      "- **Apples Used:** 20 apples\n",
      "- **Remaining Apples:** 23 - 20 = 3 apples\n",
      "- **New Purchase:** 6 apples\n",
      "- **Final Count:** 3 + 6 = 9 apples\n",
      "\n",
      "Therefore, the final answer is that the cafeteria has 9 apples. No expert left the group as everyone agreed on the correct solution.\n"
     ]
    }
   ],
   "source": [
    "# Generate the output\n",
    "outputs = pipe(zeroshot_tot_prompt)\n",
    "print(outputs[0][\"generated_text\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "TyCw3A5GZ61D"
   },
   "source": [
    "## 6.5 输出验证"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "pi67V-xNZ8fS"
   },
   "source": [
    "### 6.5.1 Providing Examples"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "execution": {
     "iopub.execute_input": "2024-10-12T09:44:00.934486Z",
     "iopub.status.busy": "2024-10-12T09:44:00.934060Z",
     "iopub.status.idle": "2024-10-12T09:44:12.018838Z",
     "shell.execute_reply": "2024-10-12T09:44:12.018067Z",
     "shell.execute_reply.started": "2024-10-12T09:44:00.934451Z"
    },
    "executionInfo": {
     "elapsed": 24562,
     "status": "ok",
     "timestamp": 1715087067979,
     "user": {
      "displayName": "Maarten Grootendorst",
      "userId": "11015108362723620659"
     },
     "user_tz": -120
    },
    "id": "NBc_HuJQY8sh",
    "outputId": "818a31a8-7dd6-4d70-feec-5db827b491d3",
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Certainly! Below is a character profile for an RPG game in JSON format:\n",
      "\n",
      "```json\n",
      "{\n",
      "  \"characterName\": \"Aelion\",\n",
      "  \"race\": \"Elven Mage\",\n",
      "  \"class\": \"Arcane Sorcerer\",\n",
      "  \"level\": 5,\n",
      "  \"alignment\": \"Chaotic Good\",\n",
      "  \"strength\": 12,\n",
      "  \"dexterity\": 16,\n",
      "  \"constitution\": 14,\n",
      "  \"intelligence\": 20,\n",
      "  \"wisdom\": 18,\n",
      "  \"charisma\": 17,\n",
      "  \"hitPoints\": 35,\n",
      "  \"experiencePoints\": 1200,\n",
      "  \"equipment\": [\n",
      "    {\n",
      "      \"item\": \"Staff of Arcane Power\",\n",
      "      \"description\": \"A staff that amplifies the user's magical abilities.\"\n",
      "    },\n",
      "    {\n",
      "      \"item\": \"Cloak of Elvenkind\",\n",
      "      \"description\": \"A shimmering cloak that grants the wearer the ability to blend into shadows.\"\n",
      "    }\n",
      "  ],\n",
      "  \"spells\": [\n",
      "    {\n",
      "      \"name\": \"Fireball\",\n",
      "      \"level\": 3,\n",
      "      \"description\": \"Summons a ball of fire that explodes on impact, dealing damage to all enemies within a radius.\"\n",
      "    },\n",
      "    {\n",
      "      \"name\": \"Invisibility\",\n",
      "      \"level\": 2,\n",
      "      \"description\": \"Makes the caster invisible for a short duration, allowing them to move undetected.\"\n",
      "    },\n",
      "    {\n",
      "      \"name\": \"Healing Light\",\n",
      "      \"level\": 1,\n",
      "      \"description\": \"Casts a beam of light that heals the target's wounds.\"\n",
      "    }\n",
      "  ],\n",
      "  \"abilities\": [\n",
      "    {\n",
      "      \"name\": \"Arcane Resilience\",\n",
      "      \"description\": \"Reduces the damage taken from magical attacks by 10%.\"\n",
      "    },\n",
      "    {\n",
      "      \"name\": \"Shadow Step\",\n",
      "      \"description\": \"Allows the character to teleport a short distance, evading attacks and moving quickly.\"\n",
      "    }\n",
      "  ],\n",
      "  \"background\": \"Aelion was born to a family of elven mages but left their secluded village to explore the world and help those in need. Known for their unpredictable nature and love for adventure, Aelion has become a powerful sorcerer with a heart full of compassion.\"\n",
      "}\n",
      "```\n",
      "\n",
      "This JSON structure provides a comprehensive overview of the character, including their attributes, equipment, spells, abilities, and background story\n"
     ]
    }
   ],
   "source": [
    "# Zero-shot learning: Providing no examples\n",
    "zeroshot_prompt = [\n",
    "    {\"role\": \"user\", \"content\": \"Create a character profile for an RPG game in JSON format.\"}\n",
    "]\n",
    "\n",
    "# Generate the output\n",
    "outputs = pipe(zeroshot_prompt)\n",
    "print(outputs[0][\"generated_text\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "execution": {
     "iopub.execute_input": "2024-10-12T09:50:13.091961Z",
     "iopub.status.busy": "2024-10-12T09:50:13.091600Z",
     "iopub.status.idle": "2024-10-12T09:50:14.717594Z",
     "shell.execute_reply": "2024-10-12T09:50:14.717092Z",
     "shell.execute_reply.started": "2024-10-12T09:50:13.091944Z"
    },
    "executionInfo": {
     "elapsed": 3915,
     "status": "ok",
     "timestamp": 1715087091714,
     "user": {
      "displayName": "Maarten Grootendorst",
      "userId": "11015108362723620659"
     },
     "user_tz": -120
    },
    "id": "4cc1U9TKZJkM",
    "outputId": "95cc264e-95a7-48e7-bef8-59d1bcef9ea9",
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\n",
      "  \"description\": \"A skilled archer with a keen eye and steady hand, often seen roving the wilderness in search of prey or enemies to hunt down from afar.\",\n",
      "  \"name\": \"Eira Windwhisper\",\n",
      "  \"armor\": \"Leather Vest\",\n",
      "  \"weapon\": \"Longbow, Quiver of Arrows\"\n",
      "}\n"
     ]
    }
   ],
   "source": [
    "# One-shot learning: Providing an example of the output structure\n",
    "one_shot_template = \"\"\"Create a short character profile for an RPG game. Make sure to only use this format:\n",
    "\n",
    "{\n",
    "  \"description\": \"A SHORT DESCRIPTION\",\n",
    "  \"name\": \"THE CHARACTER'S NAME\",\n",
    "  \"armor\": \"ONE PIECE OF ARMOR\",\n",
    "  \"weapon\": \"ONE OR MORE WEAPONS\"\n",
    "}\n",
    "\"\"\"\n",
    "one_shot_prompt = [\n",
    "    {\"role\": \"user\", \"content\": one_shot_template}\n",
    "]\n",
    "\n",
    "# Generate the output\n",
    "outputs = pipe(one_shot_prompt)\n",
    "print(outputs[0][\"generated_text\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "jR3QSswQ2kAq"
   },
   "source": [
    "### 6.5.2 Grammar: Constrained Sampling\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-10-12T09:50:33.863820Z",
     "iopub.status.busy": "2024-10-12T09:50:33.863532Z",
     "iopub.status.idle": "2024-10-12T09:50:34.346211Z",
     "shell.execute_reply": "2024-10-12T09:50:34.345629Z",
     "shell.execute_reply.started": "2024-10-12T09:50:33.863803Z"
    },
    "id": "L06f1CYoc9Pp",
    "tags": []
   },
   "outputs": [],
   "source": [
    "import gc\n",
    "import torch\n",
    "del model, tokenizer, pipe\n",
    "\n",
    "# Flush memory\n",
    "gc.collect()\n",
    "torch.cuda.empty_cache()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "execution": {
     "iopub.execute_input": "2024-10-12T09:54:34.860377Z",
     "iopub.status.busy": "2024-10-12T09:54:34.860139Z",
     "iopub.status.idle": "2024-10-12T09:55:31.702537Z",
     "shell.execute_reply": "2024-10-12T09:55:31.701974Z",
     "shell.execute_reply.started": "2024-10-12T09:54:34.860360Z"
    },
    "executionInfo": {
     "elapsed": 6644,
     "status": "ok",
     "timestamp": 1715087512875,
     "user": {
      "displayName": "Maarten Grootendorst",
      "userId": "11015108362723620659"
     },
     "user_tz": -120
    },
    "id": "B-YykcIru20X",
    "outputId": "5754cfa9-ca0b-43bf-9e28-995ad18eaf0c",
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "462caa1e0a1146abb3b45a756f1e5f1c",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "qwen2.5-0.5b-instruct-fp16.gguf:   0%|          | 0.00/1.27G [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from llama_cpp.llama import Llama\n",
    "\n",
    "# Load Phi-3\n",
    "llm = Llama.from_pretrained(\n",
    "    repo_id=\"Qwen/Qwen2.5-0.5B-Instruct-GGUF\",\n",
    "    filename=\"*fp16.gguf\",\n",
    "    n_gpu_layers=-1,\n",
    "    n_ctx=2048,\n",
    "    verbose=False\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-10-12T09:55:40.105267Z",
     "iopub.status.busy": "2024-10-12T09:55:40.104838Z",
     "iopub.status.idle": "2024-10-12T09:57:34.735332Z",
     "shell.execute_reply": "2024-10-12T09:57:34.734657Z",
     "shell.execute_reply.started": "2024-10-12T09:55:40.105231Z"
    },
    "id": "_j9TYBVIgti6",
    "tags": []
   },
   "outputs": [],
   "source": [
    "# Generate output\n",
    "output = llm.create_chat_completion(\n",
    "    messages=[\n",
    "        {\"role\": \"user\", \"content\": \"Create a warrior for an RPG in JSON format.\"},\n",
    "    ],\n",
    "    response_format={\"type\": \"json_object\"},\n",
    "    temperature=0,\n",
    ")['choices'][0]['message'][\"content\"]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "execution": {
     "iopub.execute_input": "2024-10-12T09:57:34.736584Z",
     "iopub.status.busy": "2024-10-12T09:57:34.736376Z",
     "iopub.status.idle": "2024-10-12T09:57:34.739505Z",
     "shell.execute_reply": "2024-10-12T09:57:34.739148Z",
     "shell.execute_reply.started": "2024-10-12T09:57:34.736566Z"
    },
    "executionInfo": {
     "elapsed": 16,
     "status": "ok",
     "timestamp": 1715087538556,
     "user": {
      "displayName": "Maarten Grootendorst",
      "userId": "11015108362723620659"
     },
     "user_tz": -120
    },
    "id": "Kp7FBjFdsbHe",
    "outputId": "6f7ca54b-aca2-46c9-a04d-278baebce333",
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\n",
      "    \"name\": \"Gandalf\",\n",
      "    \"description\": \"The most powerful and feared wizard of Middle-earth, Gandalf is known for his wisdom, wit, and magical abilities.\",\n",
      "    \"background\": [\n",
      "        {\n",
      "            \"name\": \"Origin\",\n",
      "            \"description\": \"Born into a noble family in the Kingdom of Gondor\"\n",
      "        },\n",
      "        {\n",
      "            \"name\": \"Career\",\n",
      "            \"description\": \"Travelled to Middle-earth as a wizard\"\n",
      "        },\n",
      "        {\n",
      "            \"name\": \"Powers\",\n",
      "            \"description\": \"Divine Inspiration, Magical Healing\"\n",
      "        }\n",
      "    ],\n",
      "    \"characteristics\": [\n",
      "        {\n",
      "            \"name\": \"Wisdom\",\n",
      "            \"description\": \"Gandalf is known for his deep understanding of the world and its creatures. He has a keen eye for detail and can see things that others miss.\"\n",
      "        },\n",
      "        {\n",
      "            \"name\": \"Courage\",\n",
      "            \"description\": \"Gandalf is a brave and fearless wizard who never backs down from a challenge or threat.\"\n",
      "        }\n",
      "    ],\n",
      "    \"achievements\": [\n",
      "        {\n",
      "            \"name\": \"Divine Inspiration\",\n",
      "            \"description\": \"Gandalf has inspired the spirits of animals to speak in their own language, which has helped him communicate with them more effectively.\"\n",
      "        },\n",
      "        {\n",
      "            \"name\": \"Magical Healing\",\n",
      "            \"description\": \"Gandalf has healed many wounds and illnesses using his magical powers, helping others recover faster than they could on their own.\"\n",
      "        }\n",
      "    ]\n",
      "}\n"
     ]
    }
   ],
   "source": [
    "import json\n",
    "\n",
    "# Format as json\n",
    "json_output = json.dumps(json.loads(output), indent=4)\n",
    "print(json_output)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "kxf4Xexhtel_"
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "accelerator": "GPU",
  "colab": {
   "authorship_tag": "ABX9TyMRJzDTVTlBntcudlyKW5VZ",
   "gpuType": "T4",
   "provenance": []
  },
  "kernelspec": {
   "display_name": "hands-on-llm",
   "language": "python",
   "name": "hands-on-llm"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.15"
  },
  "widgets": {
   "application/vnd.jupyter.widget-state+json": {
    "0512bf845dda462395f58c06dfb0733d": {
     "model_module": "@jupyter-widgets/base",
     "model_module_version": "1.2.0",
     "model_name": "LayoutModel",
     "state": {
      "_model_module": "@jupyter-widgets/base",
      "_model_module_version": "1.2.0",
      "_model_name": "LayoutModel",
      "_view_count": null,
      "_view_module": "@jupyter-widgets/base",
      "_view_module_version": "1.2.0",
      "_view_name": "LayoutView",
      "align_content": null,
      "align_items": null,
      "align_self": null,
      "border": null,
      "bottom": null,
      "display": null,
      "flex": null,
      "flex_flow": null,
      "grid_area": null,
      "grid_auto_columns": null,
      "grid_auto_flow": null,
      "grid_auto_rows": null,
      "grid_column": null,
      "grid_gap": null,
      "grid_row": null,
      "grid_template_areas": null,
      "grid_template_columns": null,
      "grid_template_rows": null,
      "height": null,
      "justify_content": null,
      "justify_items": null,
      "left": null,
      "margin": null,
      "max_height": null,
      "max_width": null,
      "min_height": null,
      "min_width": null,
      "object_fit": null,
      "object_position": null,
      "order": null,
      "overflow": null,
      "overflow_x": null,
      "overflow_y": null,
      "padding": null,
      "right": null,
      "top": null,
      "visibility": null,
      "width": null
     }
    },
    "26985bad36884566a7bd4ba6f1037e80": {
     "model_module": "@jupyter-widgets/controls",
     "model_module_version": "1.5.0",
     "model_name": "DescriptionStyleModel",
     "state": {
      "_model_module": "@jupyter-widgets/controls",
      "_model_module_version": "1.5.0",
      "_model_name": "DescriptionStyleModel",
      "_view_count": null,
      "_view_module": "@jupyter-widgets/base",
      "_view_module_version": "1.2.0",
      "_view_name": "StyleView",
      "description_width": ""
     }
    },
    "2ff276f9bbab440bb036c1109c023582": {
     "model_module": "@jupyter-widgets/controls",
     "model_module_version": "1.5.0",
     "model_name": "FloatProgressModel",
     "state": {
      "_dom_classes": [],
      "_model_module": "@jupyter-widgets/controls",
      "_model_module_version": "1.5.0",
      "_model_name": "FloatProgressModel",
      "_view_count": null,
      "_view_module": "@jupyter-widgets/controls",
      "_view_module_version": "1.5.0",
      "_view_name": "ProgressView",
      "bar_style": "success",
      "description": "",
      "description_tooltip": null,
      "layout": "IPY_MODEL_e174502c4dfe4740922264d67a9a74b5",
      "max": 2,
      "min": 0,
      "orientation": "horizontal",
      "style": "IPY_MODEL_9da5552b33c1418d9e0190ec66f19c9d",
      "value": 2
     }
    },
    "33acfbdb950545a6a67a9b39be02e30b": {
     "model_module": "@jupyter-widgets/base",
     "model_module_version": "1.2.0",
     "model_name": "LayoutModel",
     "state": {
      "_model_module": "@jupyter-widgets/base",
      "_model_module_version": "1.2.0",
      "_model_name": "LayoutModel",
      "_view_count": null,
      "_view_module": "@jupyter-widgets/base",
      "_view_module_version": "1.2.0",
      "_view_name": "LayoutView",
      "align_content": null,
      "align_items": null,
      "align_self": null,
      "border": null,
      "bottom": null,
      "display": null,
      "flex": null,
      "flex_flow": null,
      "grid_area": null,
      "grid_auto_columns": null,
      "grid_auto_flow": null,
      "grid_auto_rows": null,
      "grid_column": null,
      "grid_gap": null,
      "grid_row": null,
      "grid_template_areas": null,
      "grid_template_columns": null,
      "grid_template_rows": null,
      "height": null,
      "justify_content": null,
      "justify_items": null,
      "left": null,
      "margin": null,
      "max_height": null,
      "max_width": null,
      "min_height": null,
      "min_width": null,
      "object_fit": null,
      "object_position": null,
      "order": null,
      "overflow": null,
      "overflow_x": null,
      "overflow_y": null,
      "padding": null,
      "right": null,
      "top": null,
      "visibility": null,
      "width": null
     }
    },
    "680d3647069b45c0b4353548ac52ef37": {
     "model_module": "@jupyter-widgets/base",
     "model_module_version": "1.2.0",
     "model_name": "LayoutModel",
     "state": {
      "_model_module": "@jupyter-widgets/base",
      "_model_module_version": "1.2.0",
      "_model_name": "LayoutModel",
      "_view_count": null,
      "_view_module": "@jupyter-widgets/base",
      "_view_module_version": "1.2.0",
      "_view_name": "LayoutView",
      "align_content": null,
      "align_items": null,
      "align_self": null,
      "border": null,
      "bottom": null,
      "display": null,
      "flex": null,
      "flex_flow": null,
      "grid_area": null,
      "grid_auto_columns": null,
      "grid_auto_flow": null,
      "grid_auto_rows": null,
      "grid_column": null,
      "grid_gap": null,
      "grid_row": null,
      "grid_template_areas": null,
      "grid_template_columns": null,
      "grid_template_rows": null,
      "height": null,
      "justify_content": null,
      "justify_items": null,
      "left": null,
      "margin": null,
      "max_height": null,
      "max_width": null,
      "min_height": null,
      "min_width": null,
      "object_fit": null,
      "object_position": null,
      "order": null,
      "overflow": null,
      "overflow_x": null,
      "overflow_y": null,
      "padding": null,
      "right": null,
      "top": null,
      "visibility": null,
      "width": null
     }
    },
    "6d5243b8997d4f64af62900cb19489d0": {
     "model_module": "@jupyter-widgets/controls",
     "model_module_version": "1.5.0",
     "model_name": "HTMLModel",
     "state": {
      "_dom_classes": [],
      "_model_module": "@jupyter-widgets/controls",
      "_model_module_version": "1.5.0",
      "_model_name": "HTMLModel",
      "_view_count": null,
      "_view_module": "@jupyter-widgets/controls",
      "_view_module_version": "1.5.0",
      "_view_name": "HTMLView",
      "description": "",
      "description_tooltip": null,
      "layout": "IPY_MODEL_33acfbdb950545a6a67a9b39be02e30b",
      "placeholder": "​",
      "style": "IPY_MODEL_c6d9399532c14ce58189b9d038e4cfa4",
      "value": " 2/2 [00:38&lt;00:00, 18.13s/it]"
     }
    },
    "931c6ca8360a4e79924399f017f5522d": {
     "model_module": "@jupyter-widgets/controls",
     "model_module_version": "1.5.0",
     "model_name": "HTMLModel",
     "state": {
      "_dom_classes": [],
      "_model_module": "@jupyter-widgets/controls",
      "_model_module_version": "1.5.0",
      "_model_name": "HTMLModel",
      "_view_count": null,
      "_view_module": "@jupyter-widgets/controls",
      "_view_module_version": "1.5.0",
      "_view_name": "HTMLView",
      "description": "",
      "description_tooltip": null,
      "layout": "IPY_MODEL_0512bf845dda462395f58c06dfb0733d",
      "placeholder": "​",
      "style": "IPY_MODEL_26985bad36884566a7bd4ba6f1037e80",
      "value": "Loading checkpoint shards: 100%"
     }
    },
    "977271302f104ebdb11c0a2a8183cc93": {
     "model_module": "@jupyter-widgets/controls",
     "model_module_version": "1.5.0",
     "model_name": "HBoxModel",
     "state": {
      "_dom_classes": [],
      "_model_module": "@jupyter-widgets/controls",
      "_model_module_version": "1.5.0",
      "_model_name": "HBoxModel",
      "_view_count": null,
      "_view_module": "@jupyter-widgets/controls",
      "_view_module_version": "1.5.0",
      "_view_name": "HBoxView",
      "box_style": "",
      "children": [
       "IPY_MODEL_931c6ca8360a4e79924399f017f5522d",
       "IPY_MODEL_2ff276f9bbab440bb036c1109c023582",
       "IPY_MODEL_6d5243b8997d4f64af62900cb19489d0"
      ],
      "layout": "IPY_MODEL_680d3647069b45c0b4353548ac52ef37"
     }
    },
    "9da5552b33c1418d9e0190ec66f19c9d": {
     "model_module": "@jupyter-widgets/controls",
     "model_module_version": "1.5.0",
     "model_name": "ProgressStyleModel",
     "state": {
      "_model_module": "@jupyter-widgets/controls",
      "_model_module_version": "1.5.0",
      "_model_name": "ProgressStyleModel",
      "_view_count": null,
      "_view_module": "@jupyter-widgets/base",
      "_view_module_version": "1.2.0",
      "_view_name": "StyleView",
      "bar_color": null,
      "description_width": ""
     }
    },
    "c6d9399532c14ce58189b9d038e4cfa4": {
     "model_module": "@jupyter-widgets/controls",
     "model_module_version": "1.5.0",
     "model_name": "DescriptionStyleModel",
     "state": {
      "_model_module": "@jupyter-widgets/controls",
      "_model_module_version": "1.5.0",
      "_model_name": "DescriptionStyleModel",
      "_view_count": null,
      "_view_module": "@jupyter-widgets/base",
      "_view_module_version": "1.2.0",
      "_view_name": "StyleView",
      "description_width": ""
     }
    },
    "e174502c4dfe4740922264d67a9a74b5": {
     "model_module": "@jupyter-widgets/base",
     "model_module_version": "1.2.0",
     "model_name": "LayoutModel",
     "state": {
      "_model_module": "@jupyter-widgets/base",
      "_model_module_version": "1.2.0",
      "_model_name": "LayoutModel",
      "_view_count": null,
      "_view_module": "@jupyter-widgets/base",
      "_view_module_version": "1.2.0",
      "_view_name": "LayoutView",
      "align_content": null,
      "align_items": null,
      "align_self": null,
      "border": null,
      "bottom": null,
      "display": null,
      "flex": null,
      "flex_flow": null,
      "grid_area": null,
      "grid_auto_columns": null,
      "grid_auto_flow": null,
      "grid_auto_rows": null,
      "grid_column": null,
      "grid_gap": null,
      "grid_row": null,
      "grid_template_areas": null,
      "grid_template_columns": null,
      "grid_template_rows": null,
      "height": null,
      "justify_content": null,
      "justify_items": null,
      "left": null,
      "margin": null,
      "max_height": null,
      "max_width": null,
      "min_height": null,
      "min_width": null,
      "object_fit": null,
      "object_position": null,
      "order": null,
      "overflow": null,
      "overflow_x": null,
      "overflow_y": null,
      "padding": null,
      "right": null,
      "top": null,
      "visibility": null,
      "width": null
     }
    }
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
